package cn.itcast.jx.action.cargo;

import java.util.List;

import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.jx.action.BaseAction;
import cn.itcast.jx.domain.ContractProduct;
import cn.itcast.jx.domain.Factory;
import cn.itcast.jx.service.ContractProductService;
import cn.itcast.jx.service.FactoryService;
import cn.itcast.jx.util.Page;

public class ContractProductAction extends BaseAction implements ModelDriven<ContractProduct>{
	
	private ContractProductService contractProductService;

	private FactoryService factoryService;
	
	public void setFactoryService(FactoryService factoryService) {
		this.factoryService = factoryService;
	}
	private ContractProduct model = new ContractProduct();
	
	private String menuType = "contractProduct";
	
	public String getMenuType() {
		return menuType;
	}

	public void setMenuType(String menuType) {
		this.menuType = menuType;
	}
	private Page page = new Page();
	public void setContractProductService(ContractProductService contractProductService) {
		this.contractProductService = contractProductService;
	}
	
	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String list() {
		String hql = "from ContractProduct";
		contractProductService.findPage(hql, page, ContractProduct.class, null);
		page.setUrl("contractAction_list?cwhich=contract");
		push(page);
		return "list";
	}
	
	public String toview() {
		ContractProduct contract = contractProductService.get(ContractProduct.class, model.getId());
		push(contract);
		
		return "toview";
	}
	
	public String toupdate() {
		ContractProduct contract = contractProductService.get(ContractProduct.class, model.getId());
		String hql = "from Factory where ctype like '货物' and state = 1";
		List<Factory> factoryList = factoryService.find(hql, Factory.class, null);
		put("factoryList", factoryList);
		push(contract);
		return "toupdate";
	}
	public String update() {
		
		ContractProduct contractProduct = contractProductService.get(ContractProduct.class, model.getId());
		
		contractProduct.setFactory(model.getFactory());
		contractProduct.setProductImage(model.getProductImage());
		contractProduct.setCnumber(model.getCnumber());
		contractProduct.setLoadingRate(model.getLoadingRate());
		contractProduct.setPrice(model.getPrice());
		contractProduct.setProductDesc(model.getProductDesc());
		contractProduct.setProductNo(model.getProductNo());
		contractProduct.setPackingUnit(model.getPackingUnit());
		contractProduct.setBoxNum(model.getBoxNum());
		contractProduct.setOrderNo(model.getOrderNo());
		contractProduct.setProductRequest(model.getProductRequest());
		
		contractProductService.saveOrUpdate(model);
		return SUCCESS;
	}
	public String tocreate() {
		String hql = "from Factory where ctype like '货物' and state = 1";
		List<Factory> factoryList = factoryService.find(hql, Factory.class, null);
		//当前购销合同下所有的货物列表
		String hql2 = "from ContractProduct where contract.id=?";
		contractProductService.findPage(hql2, page, ContractProduct.class, new String[]{model.getContract().getId()});
		page.setUrl("contractProductAction_tocreate");
		push(page);
		put("factoryList", factoryList);
		
		return "tocreate";
	}
	public String insert() {
		contractProductService.saveOrUpdate(model);
		return SUCCESS;
	}
	
	
	public String delete() {
		contractProductService.delete(ContractProduct.class,model.getId());
		return SUCCESS;
	}


	
	@Override
	public ContractProduct getModel() {
		// TODO Auto-generated method stub
		return model;
	}
}
